package richbar.com.richbar.activity.me;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import richbar.com.richbar.R;
import richbar.com.richbar.adapter.order.MainAdapter;
import richbar.com.richbar.bean.CollectDelResult;
import richbar.com.richbar.fragment.collect.GoodsFragment;
import richbar.com.richbar.fragment.collect.ShopFragmet;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;

/**
 * Created by QiYang on 2018/11/30.
 */

public class CollectAc extends BaseActivity {
    @BindView(R.id.layout_tab)
    TabLayout layoutTab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.delete)
    TextView delete;
    private MainAdapter adapter;
    private GoodsFragment goodsFragment = new GoodsFragment();
    private ShopFragmet shopFragmet = new ShopFragmet();
    private int pos = 0;
    private LoadingDialog loadingDialog;

    @Override
    public int initLayout() {
        return R.layout.ac_collect;
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.e("ADAAAAAAAAAA",pos);
                if (StringUtil.getStr(rightTv).equals("完成")){
                    rightTv.setText("编辑");
                    delete.setVisibility(View.GONE);
                    if (pos == 0){
                        goodsFragment.noEditor();
                    }else {
                        shopFragmet.noEditor();
                    }
                }else {
                    rightTv.setText("完成");
                    delete.setVisibility(View.VISIBLE);
                    goodsFragment.refresh();
                    shopFragmet.refresh();
                    if (pos == 0){
                        goodsFragment.editor();
                    }else {
                        shopFragmet.editor();
                    }
                }
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete.setVisibility(View.GONE);
                if (pos == 0){
                    LogUtil.e("AMMMMMMMMM",goodsFragment.getSelects());
                    if (StringUtil.isEmpty(goodsFragment.getSelects())){
                        return;
                    }
                    unCollect("1",goodsFragment.getSelects());
                }else {
                    if (StringUtil.isEmpty(shopFragmet.getSelects())){
                        return;
                    }
                    LogUtil.e("AMMMMMMMMM:",shopFragmet.getSelects());
                    unCollect("2",shopFragmet.getSelects());
                }
            }
        });
    }

    @Override
    public void initData() {
        loadingDialog = new LoadingDialog(this,0);
        String[] titles = {"商品", "店铺"};
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(goodsFragment);
        fragments.add(shopFragmet);
        adapter = new MainAdapter(titles, fragments, getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);
        layoutTab.setupWithViewPager(viewpager);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private void unCollect(final String type, String collectId){
        loadingDialog.show();
        NetWorkUtil.delCollect(type,collectId, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final CollectDelResult result = MyGson.getInstance().fromJson(data, CollectDelResult.class);
                if (checkIsOk(result)){
                    loadingDialog.dismiss();
                    if (result.getData().getResult() == 1){
                        ToastUtil.showToast(CollectAc.this,"删除成功");
                        goodsFragment.refresh();
                        shopFragmet.refresh();
                        shopFragmet.noEditor();
                        goodsFragment.noEditor();
                        rightTv.setText("编辑");
                    }else {
                        ToastUtil.showToast(CollectAc.this,"删除失败");
                    }
                }
            }
        });
    }

}
